﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using VQWEB.administrator.sources;
using System.Web.Script.Serialization;
using VQData;
using VQWEB.common;

namespace VQWEB.administrator.handlers
{
    /// <summary>
    /// Summary description for KhoiHandler
    /// </summary>
    public class hn_KhoiHandler : IHttpHandler, IRouteHandler
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
            #region Khoi
            string add_khoi = context.Request["add_khoi"];
            if (!string.IsNullOrEmpty(add_khoi))
            {
                BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                var obj = bussiness.InsertOrUpdate<hn_khoi>(add_khoi);
                context.Response.Write("Thực thi thành công !");
            }
            string view_khoi = context.Request["view_khoi"];
            if (!string.IsNullOrEmpty(view_khoi))
            {
                string Id = context.Request["Id"];
                RenderSingle(context, Convert.ToInt32(Id));
            }
            string list_khoi = context.Request["list_khoi"];
            if (!string.IsNullOrEmpty(list_khoi))
            {
                RenderList(context);
            }

            string delete_khoi = context.Request["delete_khoi"];
            if (!string.IsNullOrEmpty(delete_khoi))
            {
                BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                bussiness.Delete<hn_khoi>(delete_khoi);
                RenderList(context);
            }

            string lockunlock_khoi = context.Request["lockunlock_khoi"];
            if (!string.IsNullOrEmpty(lockunlock_khoi))
            {
                BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                bussiness.LockUnLock<hn_khoi>(lockunlock_khoi, "khoi_active");
                RenderList(context);
            }
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        private void RenderSingle(HttpContext context, int Id)
        {
            DataLayer<HuongNghiepModelDataContext> layer = new DataLayer<HuongNghiepModelDataContext>();
            object obj = layer.Get<hn_khoi>(
               k => k.khoi_id.Equals(Id),
               k => new
               {
                   id = k.khoi_id,
                   ten = k.khoi_ten,
                   active = k.khoi_active
               });
            string json = serializer.Serialize(obj);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderList(HttpContext context)
        {
            DataLayer<HuongNghiepModelDataContext> layer = new DataLayer<HuongNghiepModelDataContext>();
            IList<object> list = layer.List<hn_khoi>(
                null,
                null,
                null,
                k => k.khoi_ten,
                k => new
               {
                   id = k.khoi_id,
                   ten = k.khoi_ten,
                   active = k.khoi_active
               });
            string json = serializer.Serialize(list);
            context.Request.ContentType = "aplication/json";
            context.Response.Write(json);
        }
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return this;
        }
    }
}